﻿// ------------------------------------------------------------------------------
// <copyright file="EnumerableExtensions.cs" company="MyCompany.com">
//  Copyright © MyCompany.com. All rights reserved.
// </copyright>
// <author>Me</author>
// ------------------------------------------------------------------------------

namespace BTerrariaMod.Sugar
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Extension methods for IEnumberable
    /// </summary>
    public static class EnumerableExtensions
    {
        /// <summary>
        /// Joins the index.
        /// </summary>
        /// <typeparam name="T">The Type this method is operating on.</typeparam>
        /// <param name="items">The items.</param>
        /// <returns>Returns a list of Tuples with the index and item.</returns>
        public static IEnumerable<Tuple<int, T>> JoinIndex<T>(this IEnumerable<T> items)
        {
            var x = -1;
            return items.Select(i =>
                                    {
                                        x++;
                                        return Tuple.Create(x, i);
                                    });
        }

        /// <summary>
        /// This method comes from SO:
        /// http://stackoverflow.com/a/833477/57883
        /// </summary>
        /// <typeparam name="T">The type this method is operating on.</typeparam>
        /// <param name="source">The object we are looking for</param>
        /// <param name="list">An array of objects to look for source In</param>
        /// <returns><c>true</c> if the source was found in list; otherwise <c>false</c>.</returns>
        public static bool In<T>(this T source, params T[] list)
        {
            if (null == source)
            {
                throw new ArgumentNullException("source");
            }

            return list.Contains(source);
        }
    }
}